package org.excample.controller;

import org.excample.entity.PageBean;
import org.excample.entity.Quality;
import org.excample.entity.Result;
import org.excample.entity.User;
import org.excample.service.QualityService;
import org.excample.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class QualityController {

    @Autowired
    @SuppressWarnings("all")
    private QualityService qualityService;
    @PostMapping("/add")
    public Result add(@RequestBody @Validated Quality quality) {
        qualityService.insertQuality(quality);;
        return Result.success();
    }

    @GetMapping("list")
    public Result select(Integer pageNum ,Integer pageSize) {
        PageBean<Quality> pb = qualityService.select(pageNum, pageSize);
        return Result.success(pb);

    }

    @GetMapping("userinfo")
    public Result userInfo(@RequestHeader(name="Authorization") String token){

        Map<String,Object> map = JwtUtil.parseToken(token);
        String name = (String) map.get("name");
        User user = QualityService.findName(name);
        return Result.success(user);
    }
}